package light.mvc.service.wxn.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import light.mvc.dao.BaseDaoI;
import light.mvc.pageModel.base.PageFilter;
import light.mvc.model.wxn.AnalysisUserBO;
import light.mvc.model.wxn.CumulateUserBO;
import light.mvc.pageModel.wxn.AnalysisUserVO;
import light.mvc.pageModel.wxn.CumulateUserVO;
import light.mvc.service.wxn.CumulateUserServiceI;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


@Service
public class CumulateUserServiceImpl implements CumulateUserServiceI {

   @Autowired
   private BaseDaoI<CumulateUserBO> CumulateUserDao;

   @Override
   public void add(CumulateUserVO r) {
       CumulateUserBO t = new CumulateUserBO();
       t.setCumulateUser(r.getCumulateUser());
       t.setRefDate(r.getRefDate());
       t.setNote(r.getNote());
       t.setWeiName(r.getWeiName());
       t.setAppid(r.getAppid());
       CumulateUserDao.save(t);
   }

   @Override
   public void delete(Long id) {
       CumulateUserBO t = CumulateUserDao.get(CumulateUserBO.class, id);
       CumulateUserDao.delete(t);
   }

   @Override
   public void edit(CumulateUserVO r) {
       CumulateUserBO t = CumulateUserDao.get(CumulateUserBO.class, r.getId());
       t.setCumulateUser(r.getCumulateUser());
       t.setRefDate(r.getRefDate());
       t.setNote(r.getNote());
       t.setWeiName(r.getWeiName());
       t.setAppid(r.getAppid());
       CumulateUserDao.update(t);
   }

   @Override
   public CumulateUserVO get(Long id) {
       CumulateUserBO t = CumulateUserDao.get(CumulateUserBO.class, id);
       CumulateUserVO r = new CumulateUserVO();
       r.setId(id);
       r.setId(t.getId());
       r.setCumulateUser(t.getCumulateUser());
       r.setRefDate(t.getRefDate());
       r.setNote(t.getNote());
       r.setWeiName(t.getWeiName());
       r.setAppid(t.getAppid());
       return r;
   }

   @Override
   public List<CumulateUserVO> dataGrid(CumulateUserVO cumulateUser, PageFilter ph) {
       List<CumulateUserVO> ul = new ArrayList<CumulateUserVO>();
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from CumulateUserBO t ";
       List<CumulateUserBO> l = CumulateUserDao.find(hql + whereHql(cumulateUser, params) + orderHql(ph), params, ph.getPage(), ph.getRows());
       for (CumulateUserBO t : l) {
           CumulateUserVO u = new CumulateUserVO();
           BeanUtils.copyProperties(t, u);
           ul.add(u);
       }
       return ul;
   }

   @Override
   public Long count(CumulateUserVO cumulateUser, PageFilter ph) {
       Map<String, Object> params = new HashMap<String, Object>();
       String hql = " from CumulateUserBO t ";
       return CumulateUserDao.count("select count(*) " + hql + whereHql(cumulateUser, params), params);
   }

   private String whereHql(CumulateUserVO cumulateUser, Map<String, Object> params) {
       String hql = "";
       if (cumulateUser != null) {
           hql += " where 1=1 ";
           if (cumulateUser.getId() != null) {
               hql += " and t.name like :name";
               params.put("name", "%%" + cumulateUser.getId() + "%%");
           }
       }
       return hql;
   }

   private String orderHql(PageFilter ph) {
       String orderString = "";
       if ((ph.getSort() != null) && (ph.getOrder() != null)) {
           orderString = " order by t." + ph.getSort() + " " + ph.getOrder();
       }
       return orderString;
   }
   
   
   @Override
   public boolean isThere(CumulateUserVO cumulateUserVO){
	   Date date = cumulateUserVO.getRefDate();
	   SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
	   List<CumulateUserBO> listCumulateUserBO =  CumulateUserDao.find("from CumulateUserBO  where refDate='"+sdf.format(date)+"'");
	   if(listCumulateUserBO.size() >0)
		   return true;
	   else
		   return false;
   }
}